package edu.scau.mis.pos.service.impl;

import edu.scau.mis.pos.domain.Product;
import edu.scau.mis.pos.mapper.ProductMapper;
import edu.scau.mis.pos.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> listAllProduct() {
        return productMapper.listAllProduct();
    }

    @Override
    public List<Product> selectProductByPage(Product product) {
        return productMapper.selectProductByPage(product);
    }

    @Override
    public List<Product> selectProductByPrice(int minPrice, int maxPrice) {
        return productMapper.selectProductByPrice(minPrice, maxPrice);
    }

    @Override
    public Product selectProductBySn(String productSn) {
        return productMapper.selectProductBySn(productSn);
    }

    @Override
    public List<Product> selectProductByName(String productName) {
        return productMapper.selectProductByName(productName);
    }

    @Override
    public int addProduct(Product product) {
        // 生成新的商品编码
        String productSn = generateNextProductSn();
        product.setProductSn(productSn);

        return productMapper.insertProduct(product);
    }

    @Override
    public int deleteProductBySn(String productSn) {
        return productMapper.deleteProductBySn(productSn);
    }

    @Override
    public int deleteProductBySnBatch(List<String> productSnList) {
        return productMapper.deleteProductBySnBatch(productSnList);
    }

    /**
     * 生成下一个商品编码
     * 编码格式：P001 -> P002 -> P003 ...
     */
    private String generateNextProductSn() {
        String maxProductSn = productMapper.getMaxProductSn(); // 查询数据库当前最大编号
        if (maxProductSn == null || maxProductSn.isEmpty()) {
            return "P001"; // 如果数据库里没有商品，返回 P001
        }

        // 提取数字部分
        Pattern pattern = Pattern.compile("P(\\d+)");
        Matcher matcher = pattern.matcher(maxProductSn);

        if (matcher.matches()) {
            int nextNumber = Integer.parseInt(matcher.group(1)) + 1;
            return String.format("P%03d", nextNumber); // 格式化为 Pxxx（3位数字）
        } else {
            return "P001"; // 万一格式不对，默认返回 P001
        }
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.updateProduct(product);
    }

    @Override
    public Product getProductBySn(String productSn) {
        return productMapper.getProductBySn(productSn);
    }

    @Override
    public String getCategoryNameById(String categoryId) {
        return productMapper.getCategoryNameById(categoryId);
    }

    @Override
    public void increaseStock(String productSn, Integer quantity) {
        // 假设 productMapper 有方法增加库存
        productMapper.increaseStock(productSn, quantity);
    }

    @Override
    public void decreaseSalesVolume(String productSn, Integer quantity) {
        // 假设 productMapper 有方法减少销售量
        productMapper.decreaseSalesVolume(productSn, quantity);
    }
}
